package com.example.zhu29.bledemo.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.zhu29.bledemo.R;
import com.example.zhu29.bledemo.adapter.MyBLEListAdapter;
import com.example.zhu29.bledemo.interFace.IBleScanListener;
import com.example.zhu29.bledemo.interFace.IBleStateListener;
import com.example.zhu29.bledemo.interFace.IPermissionsListener;
import com.example.zhu29.bledemo.util.ZBleManager;
import com.example.zhu29.bledemo.util.ZPermissionUtil;
import com.inuker.bluetooth.library.search.SearchResult;

/**
 * Blood glucose measurement
 */
public class BloodSugarActivity extends AppCompatActivity implements View.OnClickListener, IPermissionsListener, IBleStateListener {

    private RecyclerView mRv;
    private TextView mTvStart;
    private LinearLayout mLLStart;
    private ProgressBar mProgress;
    private MyBLEListAdapter mAdapter;
    private boolean isStart = false;
    private ZBleManager mBleManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bld_sugar);
        findID();
        initList();
        initListener();

        if (!ZPermissionUtil.getInstance().isPermissions(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            ZPermissionUtil.getInstance().requestPermissions(this, this, Manifest.permission.ACCESS_COARSE_LOCATION);
        } else {
            isStart = true;
            initScan();
        }
    }

    private void initListener() {
        mRv.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(BloodSugarActivity.this, SocketActivity.class);
                SearchResult device = mAdapter.getData().get(position);
                intent.putExtra("device", device);
                startActivity(intent);
            }
        });
        mLLStart.setOnClickListener(this);
    }

    private void initList() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRv.setLayoutManager(linearLayoutManager);
        mRv.addItemDecoration(new DividerItemDecoration(this, linearLayoutManager.getOrientation()));
        mAdapter = new MyBLEListAdapter(null);
        mRv.setAdapter(mAdapter);
    }

    private void findID() {
        mRv = findViewById(R.id.rv_list);
        mTvStart = findViewById(R.id.tv_start);
        mLLStart = findViewById(R.id.ll_start);
        mProgress = findViewById(R.id.progress);
    }

    private void initScan() {
        mBleManager = ZBleManager.getInstance();
        mBleManager.initBle(this);
        if (mBleManager.isSupportBle()) {
            if (!mBleManager.isBlueOpen()) {
                mBleManager.openBluetooth(this);
            } else {
                bleScan();
            }
//            String[] names = {"Bioland-BGM", "Bioland", "bioland"};
        } else {
            Toast.makeText(this, getString(R.string.device_not_support), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onBleOpen() {
        bleScan();
    }

    private void bleScan() {
        mBleManager.scan(new IBleScanListener() {
            @Override
            public void onScanStarted() {
                mAdapter.setNewData(null);
                mTvStart.setText(R.string.stop);
                mProgress.setVisibility(View.VISIBLE);
                Log.e("zdw", "onScanStarted");
            }

            @Override
            public void onScanCanceled() {

            }

            @Override
            public void onScanStop() {
                mTvStart.setText(R.string.start);
                mProgress.setVisibility(View.GONE);
                Log.e("zdw", "onScanFinished");
            }

            @Override
            public void onDeviceFounded(SearchResult device) {
                if (device == null) return;
                if (!device.getName().contains("Bioland-BGM")) return;
                if (mAdapter.getData().contains(device)) return;
                mAdapter.addData(device);
                Log.e("zdw", "onScanning");
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_start:
                if (!isStart) {
                    isStart = true;
                    initScan();
                } else {
                    isStart = false;
                    mProgress.setVisibility(View.GONE);
                    mTvStart.setText(R.string.start);
                }
                break;
        }
    }

    @Override
    public void onPermissionsSuccess() {
        isStart = true;
        initScan();

    }

    @Override
    public void onPermissionsFail() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ZBleManager.getInstance().cancelScan();
    }
}
